package com.huatai.manage.controller;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.huatai.common.api.R;
import com.huatai.manage.aop.annotation.TrialIdIsExist;
import com.huatai.manage.query.PageQuery;
import com.huatai.manage.query.RemoveQuery;
import com.huatai.manage.entity.TbInfoSubjectEntity;
import com.huatai.manage.service.TbInfoSubjectService;
import com.huatai.manage.utils.ComUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 试验对象关联; 前端控制器
 * </p>
 *
 * @author luoxc
 * @since 2023-03-21
 */
@RestController
@RequestMapping("infoSubject")
@Api(value = "试验对象关联", tags = "试验对象关联")
public class TbInfoSubjectController {

	@Autowired
	private TbInfoSubjectService tbInfoSubjectService;

	@ApiOperationSupport(order = 1, includeParameters = {
		"pageQuery.current",
		"pageQuery.size",
		"pageQuery.data.trialName",
		"pageQuery.data.subjectName",
	})
	@PostMapping("pageList")
	@ApiOperation(value = "分页查询数据", notes = "分页查询数据")
	public R pageList(@RequestBody PageQuery<TbInfoSubjectEntity> pageQuery) {
		return R.data(this.tbInfoSubjectService.pageList(pageQuery));
	}

	@GetMapping("list")
	@ApiOperation(value = "查询所有数据", notes = "查询所有数据")
	public R list() {
		return R.data(this.tbInfoSubjectService.infoList());
	}

	@PostMapping("listByTrialId")
	@ApiOperation(value = "根据试验id查询对象关联列表", notes = "根据试验id查询对象关联列表")
	public R getByTrialId(@ApiParam(name = "trialId", value = "试验id", required = true) @RequestParam("trialId") String trialId) {
		return R.data(this.tbInfoSubjectService.getByTrialId(trialId));
	}

	@PostMapping("getById")
	@ApiOperation(value = "通过主键id查询单条数据", notes = "通过主键id查询单条数据")
	public R getOne(@ApiParam(name = "id", value = "主键id", required = true) @RequestParam("id") String id) {
		return R.data(this.tbInfoSubjectService.getById(id));
	}

	@ApiOperationSupport(order = 2, includeParameters = {
		"tbInfoSubjectEntity.trialId",
		"tbInfoSubjectEntity.subjectId",
		"tbInfoSubjectEntity.state"
	})
	@TrialIdIsExist(remark = "校验trialId在数据库中是否存在")
	@PostMapping("insert")
	@ApiOperation(value = "新增数据", notes = "新增数据")
	public R insert(@RequestBody @Valid TbInfoSubjectEntity tbInfoSubjectEntity) {
		return R.status(this.tbInfoSubjectService.save(tbInfoSubjectEntity.setInfoSubjectId(ComUtil.getUUID())));
	}

	@ApiOperationSupport(order = 4, includeParameters = {
		"tbInfoSubjectEntity.id",
		"tbInfoSubjectEntity.trialId",
		"tbInfoSubjectEntity.subjectId",
		"tbInfoSubjectEntity.state"
	})
	@TrialIdIsExist(remark = "校验trialId在数据库中是否存在")
	@PostMapping("update")
	@ApiOperation(value = "修改数据", notes = "修改数据")
	public R update(@RequestBody @Valid TbInfoSubjectEntity tbInfoSubjectEntity) {
		return R.status(this.tbInfoSubjectService.updateById(tbInfoSubjectEntity));
	}

	@PostMapping("state")
	@ApiOperation(value = "修改状态", notes = "修改状态")
	public R update(@ApiParam(name = "id", value = "id", required = true) @RequestParam("id") String id,
					@ApiParam(name = "state", value = "状态", required = true) @RequestParam("state") String state) {
		return R.status(this.tbInfoSubjectService.updateState(id, state));
	}

	@PostMapping("removes")
	@ApiOperation(value = "删除数据", notes = "删除数据")
	public R removes(@RequestBody RemoveQuery remove) {
		return R.status(this.tbInfoSubjectService.removeBatchByIds(remove.getIds()));
	}

}
